import ConstDef from '@/js/ConstDef'
import layoutService from '@/api/LayoutService'
import cacheManage from '@/cache/CacheManage'
import dbMapService from '@/api/DbMapService'
import ExtBase from "@/views/basic/ext/ExtBase";
class NdLayoutFilterExt extends ExtBase {
  supportEvent (functionCode) {
    if (functionCode === ConstDef.FUNCTION_CODE_CREATE_DEFAULT_ENTRY_LAYOUT ||
      functionCode === ConstDef.FUNCTION_CODE_CREATE_DEFAULT_CONDITION_LAYOUT ||
      functionCode === ConstDef.FUNCTION_CODE_CREATE_DEFAULT_RESULT_LAYOUT ||
      functionCode === ConstDef.FUNCTION_CODE_CREATE_DEFAULT_DB_MAP) {
      return true;
    } else {
      return false;
    }
  }

  async handleEvent(self, event) {
    const fvData = event.fvData;
    const fdLayout = fvData.fdLayout;
    const functionCode = fdLayout.functionCode;
    if (functionCode === ConstDef.FUNCTION_CODE_CREATE_DEFAULT_ENTRY_LAYOUT ||
      functionCode === ConstDef.FUNCTION_CODE_CREATE_DEFAULT_CONDITION_LAYOUT ||
      functionCode === ConstDef.FUNCTION_CODE_CREATE_DEFAULT_RESULT_LAYOUT) {
      this.createDefaultEntry(self, event, functionCode);
    } else if (functionCode === ConstDef.FUNCTION_CODE_CREATE_DEFAULT_DB_MAP) {
      this.createDefaultDbMapNv(self, event, functionCode)
    }
  }

  async createDefaultEntry(self, event, functionCode) {
    const fvData = event.fvData;
    const nv = fvData.getNv();
    const ndCode = nv.ndCode;
    const commonInfo = cacheManage.getDefaultCommonInfo();
    let nvMsg;
    if (functionCode === ConstDef.FUNCTION_CODE_CREATE_DEFAULT_ENTRY_LAYOUT) {
      nvMsg = await layoutService.createDefaultEntryLayoutNv(commonInfo, ndCode);
    } else if (functionCode === ConstDef.FUNCTION_CODE_CREATE_DEFAULT_CONDITION_LAYOUT) {
      nvMsg = await layoutService.createFilterConditionLayoutNv(commonInfo, ndCode);
    } else if (functionCode === ConstDef.FUNCTION_CODE_CREATE_DEFAULT_RESULT_LAYOUT) {
      nvMsg = await layoutService.createFilterResultLayoutNv(commonInfo, ndCode);
    } else {
      return;
    }

    if (!nvMsg.success) return;
    const layoutNv = nvMsg.value;
    const tabPage = {
      name: "layout new",
      title: "布局新建",
      moduleCode: ConstDef.MODULE_CODE_ENTRY,
      config: {
        nvId: 0,
        ndCode: ConstDef.ND_CODE_LAYOUT,
        layoutCode: 'layoutTreeLayout',
        nv: layoutNv,
        action: 'create'
      }
    };
    self.$emit("tabPage", tabPage);
  }

  async createDefaultDbMapNv(self, event, functionCode) {
    const fvData = event.fvData;
    const nv = fvData.getNv();
    const ndCode = nv.ndCode;
    const commonInfo = cacheManage.getDefaultCommonInfo();
    const nvMsg = await dbMapService.createDefaultDbMapNv(commonInfo, ndCode);
    if (!nvMsg.success) return;
    const dbMapNv = nvMsg.value;
    const tabPage = {
      name: "dbMap new",
      title: "数据库映射新建",
      moduleCode: ConstDef.MODULE_CODE_ENTRY,
      config: {
        nvId: 0,
        ndCode: ConstDef.ND_CODE_TYPE_MAPPING_ND,
        layoutCode: 'dbMappingLayout',
        nv: dbMapNv,
        action: 'create'
      }
    };
    self.$emit("tabPage", tabPage);
  }
}
const ndLayoutFilterExt = new NdLayoutFilterExt();
export default ndLayoutFilterExt;
